#include "glhead.h"
#include "render.h"
#include "global.h"
#include "control.h"
#include "init.h"
#include "ConfigCoApp.h"
#include "vector2d.h"

void init(void){
	glClearColor(0,0,0,1);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);
	initialize();
}

void display(void){
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	render_all();

	glutSwapBuffers();
}

void reshape(int w,int h){
	glViewport(0,0,w,h);
	cur_width = w;
	cur_height = h;
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D( 0,(GLfloat)w,0,(GLfloat)h);
	glGetFloatv(GL_PROJECTION_MATRIX,orth);

	glLoadIdentity();
	gluPerspective(asin(0.2)*2*GRA_PI,(GLfloat)w/(GLfloat)h,0.1,48);
	glGetFloatv(GL_PROJECTION_MATRIX,pers);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

ConfigCoApp::ConfigCoApp(void)
{
	max_life = DEF_MAX_LIFE;
	life = 0;
	cur_mode = CAB_DOING;

	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutInitWindowSize(Window_Width,Window_Height);
	glutInitWindowPosition(Offset_X,Offset_Y);
	glutCreateWindow("config coodinates");
	init();
	glutDisplayFunc(display);
	glutIdleFunc(display);	
	glutReshapeFunc(reshape);
	//glutTimerFunc(REFRESH_TIME, refresh_all, 1);
	glutMotionFunc(motion);
	//glutPassiveMotionFunc(passive_motion);
	glutMouseFunc(mouse); 
	glutKeyboardFunc(keyboard);
	glutSpecialFunc(special_keys);
}

void ConfigCoApp::touch_down(TouchData data)
{
	press(data);

}

void ConfigCoApp::touch_up(TouchData data)
{
	life = 0;
	glo->set_cur_p_percent(0);

}
void ConfigCoApp::touch_update(TouchData data)
{
	press(data);

}

void ConfigCoApp::touch_event(const std::vector<TouchData> &tdata)
{
	int i,n=tdata.size();
	float max_area = -1;
	int biggest_touch_inx = 0;
	for(i=0;i<n;i++){
		if(tdata[i].area > max_area){
			biggest_touch_inx = i;
			max_area = tdata[i].area;
		}
	}
	if(max_area>0){
		switch (tdata[biggest_touch_inx].tEvent){
		case TOUCH_EVENT:
			touch_down(tdata[biggest_touch_inx]);
			break;
		case UNTOUCH_EVENT:
			touch_up(tdata[biggest_touch_inx]);
			break;
		case UPDATE_EVENT:
			touch_update(tdata[biggest_touch_inx]);
			break;
		}
	}
}

void ConfigCoApp::press(TouchData data)
{
	vector2dd vecd;
	switch (cur_mode){
	case CAB_DONE:
		glo->cur_point.X = data.sphere_longitude;
		glo->cur_point.Y = data.sphere_latitude;
		break;
	case CAB_DOING:
		vecd.X = data.X;	vecd.Y = data.Y;
		active_p[life++] = vecd;
		glo->set_cur_p_percent((double)life/(double)max_life);
		if( life >= max_life){ 
			point_confirm();
			life = 0;
			glo->set_cur_p_percent(0);
		}
		break;
	}

}

void ConfigCoApp::point_confirm(void)
{
	int i;
	double x = 0,y = 0;
	for(i=0;i<DEF_MAX_LIFE;i++){
		x += active_p[i].X;
		y += active_p[i].Y;
	}
	if( !glo->next_rp(x/DEF_MAX_LIFE,y/DEF_MAX_LIFE)){
		cur_mode = CAB_DONE;
	}
}

void ConfigCoApp::start(void)
{
	glutMainLoop();
}

ConfigCoApp::~ConfigCoApp(void)
{

}